{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6c5e2f81",
   "metadata": {},
   "source": [
    "# AutoTS Example\n",
    "\n",
    "AutoTS是用于支持PaddleTS的自动机器学习能力组件。\n",
    "\n",
    "AutoTS 可以支持 PaddleTS 模型和 pipeline 的自动超参数选择，减少人工介入成本，降低专业门槛。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72271da9",
   "metadata": {},
   "source": [
    "## 1. 准备数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee177f3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from paddlets.datasets.repository import get_dataset\n",
    "tsdataset = get_dataset(\"UNI_WTH\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15507576",
   "metadata": {},
   "source": [
    "## 2 构造和训练\n",
    "\n",
    "通过四行代码，我们利用 MLPRegressor 初始化了一个 AutoTS 模型。 AutoTS 会在训练的过程中自动进行超参优化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "753cc40c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from paddlets.models.forecasting import MLPRegressor\n",
    "from paddlets.automl.autots import AutoTS\n",
    "autots_model = AutoTS(MLPRegressor, 96, 2)\n",
    "autots_model.fit(tsdataset, n_trials=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe17caee",
   "metadata": {},
   "source": [
    "## 3. 搜索空间\n",
    "对于超参数优化来说，你可以定义一个搜索空间。如果你没有指定一个搜索空间，我们也为每个 PaddleTS 模型内置了推荐的默认搜索空间。\n",
    "\n",
    "你可以利用搜索空间去控制你的超参的取值是如何采样的，控制值范围是多少。\n",
    "\n",
    "下面是一个指定了搜索空间的 autots pipeline 的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef2f9af5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ray.tune import uniform, qrandint, choice\n",
    "from paddlets.transform import Fill\n",
    "\n",
    "sp = {\n",
    "    \"Fill\": {\n",
    "        \"cols\": ['WetBulbCelsius'],\n",
    "        \"method\": choice(['max', 'min', 'mean', 'median', 'pre', 'next', 'zero']),\n",
    "        \"value\": uniform(0.1, 0.9),\n",
    "        \"window_size\": qrandint(20, 50, q=1)\n",
    "    },\n",
    "    \"MLPRegressor\": {\n",
    "        \"batch_size\": qrandint(16, 64, q=16),\n",
    "        \"use_bn\": choice([True, False]),\n",
    "        \"max_epochs\": qrandint(10, 50, q=10)\n",
    "    }\n",
    "}\n",
    "autots_model = AutoTS([Fill, MLPRegressor], 25, 2, search_space=sp, sampling_stride=25)\n",
    "autots_model.fit(tsdataset, n_trials=1)\n",
    "sp = autots_model.search_space()\n",
    "predicted = autots_model.predict(tsdataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9382f987",
   "metadata": {},
   "source": [
    "搜索空间定义的 API 可以参考：https://docs.ray.io/en/latest/tune/api_docs/search_space.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99782716",
   "metadata": {},
   "source": [
    "## 4. 搜索算法\n",
    "\n",
    "PaddleTS 的搜索算法是利用多个开源的超参优化库包装而成。\n",
    "\n",
    "我们内置了如下几种搜索算法：\n",
    "[“Random”, “CMAES”, “TPE”, “CFO”, “BlendSearch”, “Bayes”]\n",
    "\n",
    "对于这些超参优化库的细节，可以参考它们对应的开源文档。\n",
    "\n",
    "你可以通过下面的方式指定搜索算法：\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a87b1982",
   "metadata": {},
   "outputs": [],
   "source": [
    "autots_model = AutoTS(MLPRegressor, 96, 2, search_alg=\"CMAES\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "155780f7",
   "metadata": {},
   "source": [
    "如果搜索算法没有被指定，我们会默认使用TPE算法。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}